class Solution {
    public int minOperations(int[] nums, int x) {
        int sum = 0;
        for(int i = 0;i < nums.length;i++) {
            sum += nums[i];
        }
        int target = sum - x;
        if(target < 0) {
            return -1;
        }
        int max = -1;
        sum = 0;
        for(int left = 0,right = 0;right < nums.length;right++) {
            sum += nums[right];
            while(sum > target) {
                sum -= nums[left++];
            }
            if(sum == target) max = Math.max(max,right - left + 1);
        }
        if(max == -1) return max;
        else return nums.length - max;
    }
}